<template>
	<div class="myPage" style="">
		<div v-if="false"
			style="position: absolute;width: 100%;height: 100%;background-color: white;filter:blur(87rpx);z-index: 1;">
			<div
				style="position: absolute;width: 380rpx;height: 380rpx;background-color: lightgreen;border-radius: 50%;right: -8%;top: -14%;">

			</div>
			<div
				style="position: absolute;width: 520rpx;height: 520rpx;background-color: deepskyblue;border-radius: 50%;left:-24%;top: -14%;">

			</div>
		</div>
		<fui-nav-bar fontWeight="bold" style="" background="white" color="#000" title="账号管理" @rightClick="rightClick">
			<div
				style="border: 1px solid #eeeeee;background: white;border-radius: 32rpx;padding: 6rpx;display: flex;flex-direction: row;width: 200rpx;justify-content: space-around;">
				<fui-icon style="margin: 0rpx 8rpx;" size="43" @click="leftClick" color="black"
					name="arrowleft"></fui-icon>
				<div class="link-column" style=""></div>
				<fui-icon style="margin: 0rpx 8rpx;" size="43" @click="gotoHome" color="black" name="home"></fui-icon>
			</div>
			<template v-slot:right>

			</template>
		</fui-nav-bar>
		<div style="background-color: white;margin: 16rpx;border-radius: 16rpx;padding: 8rpx 32rpx;">


			<div @click="login(item)"
				style="display: flex;flex-direction: row;align-items: center;border-bottom:1rpx solid #e3e3e3;margin: 16rpx 0rpx;"
				v-for="(item,index) in loginAccountList">
				<image src='/static/icon/boy.png'
					style="width: 80rpx;height: 80rpx;margin-right: 16rpx;padding: 16rpx 0rpx;"></image>
				<div style="display: flex;flex-direction: column;">
					<span>{{item.nickname}} ({{item.schoolName}})</span>
					<span style="font-size: 32rpx;">{{item.username}}</span>
				</div>
			</div>
			<div @click="gotoLogin()" style="height: 96rpx;display: flex;flex-direction: row;align-items: center;">
				<fui-button @click="gotoLogin()" background="#f3f3f3" width="80rpx" height="80rpx" radius="96rpx">
					<fui-icon name="plus"></fui-icon>
				</fui-button>
				<span style="margin-left: 16rpx;">添加账号</span>
			</div>
		</div>
	</div>
	<fui-toast ref="toast"></fui-toast>
</template>

<script>
	import request from "/request/request.js"
	export default {
		data() {
			return {
				loginAccountList: []
			}
		},
		onLoad() {
			console.log("onLoad");
			this.getLoginAccountList();
		},
		methods: {
			leftClick() {
				uni.navigateBack();
			},
			gotoHome() {
				getApp().gotoHome();
			},
			gotoLogin() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},
			getLoginAccountList() {
				console.log("getLoginInfo");
				let loginAccountListStr = uni.getStorageSync('loginAccountList');
				if (loginAccountListStr != null && loginAccountListStr != '') {
					this.loginAccountList = JSON.parse(loginAccountListStr);
				}
			},
			login(loginAccount) {
				console.log(JSON.stringify(loginAccount));
				request({
					url: `/user/login`,
					method: 'POST',
					data: {
						userName: loginAccount.username,
						password: loginAccount.password
					}
				}).then((res) => {
					if (res.code == 0) {
						res.data.password = loginAccount.password;
						let loginAccountListStr = uni.getStorageSync('loginAccountList');
						let loginAccountList = [];
						if (loginAccountListStr != null && loginAccountListStr != '') {
							loginAccountList = JSON.parse(loginAccountListStr);
						}
						console.log("this.deleteAccountById:");
						loginAccountList = this.deleteAccountById(loginAccountList, res.data.id);
						loginAccountList.push(res.data);
						uni.setStorageSync('loginAccountList', JSON.stringify(loginAccountList))
						console.log("loginAccountList:");
						console.log(loginAccountList + "");
						uni.switchTab({
							url: '/pages/home/home'
						})
						uni.setStorageSync('loginInfo', JSON.stringify(res.data))
					}else{
						let loginAccountListStr = uni.getStorageSync('loginAccountList');
						let loginAccountList = [];
						if (loginAccountListStr != null && loginAccountListStr != '') {
							loginAccountList = JSON.parse(loginAccountListStr);
						}
						console.log("this.deleteAccountById:");
						loginAccountList = this.deleteAccountById(loginAccountList, loginAccount.id);
						uni.setStorageSync('loginAccountList', JSON.stringify(loginAccountList))
						// 
						uni.navigateTo({
							url:"/pages/login/login"
						})
					}
					let options = {}
					//提示信息
					options.text = res.userMsg;
					this.$refs.toast.show(options)
				}, (err) => {
					
					console.log('fail' + err);
				});

			},
			deleteAccountById(loginAccountList, id) {
				let newLoginAccountList = [];
				console.log("deleteAccountById");
				for (var i = 0; i < loginAccountList.length; i++) {
					if (loginAccountList[i].id != id) {
						console.log(loginAccountList[i].id + ";" + id);
						newLoginAccountList.push(loginAccountList[i]);
					}
				}
				return newLoginAccountList;
			},
		}
	}
</script>

<style scoped>

</style>